package com.voole.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public abstract class BaseController {

	protected String ajaxComplete(Object returnObj, HttpServletResponse response) {
		try {
			String returnStr = null;
			if (returnObj == null) {
				returnStr = "";
			} else if (returnObj == null || returnObj instanceof String || returnObj instanceof Boolean || returnObj instanceof Number) {
				returnStr = String.valueOf(returnObj);
			} else if (returnObj instanceof Collection<?>) {
				returnStr = JSONArray.fromObject(returnObj).toString();
			}else if(returnObj instanceof Boolean){
				returnStr = String.valueOf(returnObj);
			} else {
				returnStr = JSONObject.fromObject(returnObj).toString();
			}
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.write(returnStr);
			out.close();
		} catch (IOException e) {
			return null;
		}
		return null;
	}
	
}
